﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace voha_test_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void CalcTaxData(byte qNum, object sender, TextBox tbTotal, TextBox tbTotal5, TextBox tbTax)
        {
            var tbQ = (TextBox)sender;
            double q = TryConvertToFloat(tbQ.Text.Replace('.', ','), tbQ);
            double total = GetSumQ(qNum, q);
            double total5 = total * 0.05;
            double tax = q * 0.05;
            tbTotal.Text = total.ToString();
            tbTotal5.Text = total5.ToString();
            tbTax.Text = tax.ToString();
        }

        private static double TryConvertToFloat(string str, TextBox tb = null)
        {
            double f = 0;
            try
            {
                f = Convert.ToDouble(str);
                if (tb != null && tb.BackColor == Color.Red)
                    tb.BackColor = Color.White;
            }
            catch (Exception)
            {
                if (tb != null)
                    tb.BackColor = Color.Red;
                //logging?
                //throw;
            }
            return f;
        }

        private void AddZeros(object sender)
        {
            var tb = (TextBox)sender;
            if (tb.Text.IndexOf(".") == 0)
                tb.Text = @"0" + tb.Text;
        }

        private static void RestrictInput(object sender, KeyPressEventArgs e)
        {
            var tb = (TextBox) sender;
            if (Char.IsDigit(e.KeyChar) || (e.KeyChar == '.') && (tb.Text.IndexOf(".") == -1))
                return;

            if (e.KeyChar != (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

        private double GetSumQ(byte q, double current)
        {
            switch (q)
            {
                case 1:
                    return current;
                case 2:
                    return TryConvertToFloat(TbQ1.Text) + current;
                case 3:
                    return TryConvertToFloat(TbQ1.Text) + TryConvertToFloat(TbQ2.Text) + current;
                case 4:
                    return TryConvertToFloat(TbQ1.Text) + TryConvertToFloat(TbQ2.Text) + TryConvertToFloat(TbQ3.Text) + current;
                default:
                    return 0;
            }
        }

        private void TbQ1_TextChanged(object sender, EventArgs e)
        {
            AddZeros(sender);
            CalcTaxData(1, sender, TbQ1Total, TbQ1Total5, TbQ1Tax);
            TbQ2_TextChanged(sender, e);
        }

        private void TbQ2_TextChanged(object sender, EventArgs e)
        {
            AddZeros(sender);
            CalcTaxData(2, sender, TbQ2Total, TbQ2Total5, TbQ2Tax);
            TbQ3_TextChanged(sender, e);
        }

        private void TbQ3_TextChanged(object sender, EventArgs e)
        {
            AddZeros(sender);
            CalcTaxData(3, sender, TbQ3Total, TbQ3Total5, TbQ3Tax);
            TbQ4_TextChanged(sender, e);
        }

        private void TbQ4_TextChanged(object sender, EventArgs e)
        {
            AddZeros(sender);
            CalcTaxData(4, sender, TbQ4Total, TbQ4Total5, TbQ4Tax);
        }

        private void TbQ1_KeyPress(object sender, KeyPressEventArgs e)
        {
            RestrictInput(sender, e);
        }

        private void TbQ4_KeyPress(object sender, KeyPressEventArgs e)
        {
            RestrictInput(sender, e);
        }

        private void TbQ2_KeyPress(object sender, KeyPressEventArgs e)
        {
            RestrictInput(sender, e);
        }

        private void TbQ3_KeyPress(object sender, KeyPressEventArgs e)
        {
            RestrictInput(sender, e);
        }
    }
}
